package com.webbei.product.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.wabbei.fenxiao.action.data;
import com.webbei.pojo.Advertisement;
import com.webbei.pojo.Designers;
import com.webbei.product.service.ProductService;
import com.webbei.util.Arithmetic;
import com.webbei.util.Thumbnail;
import com.webbei.product.action.Product;;

public class ShowProducts {
  private ProductService ps;	
  private List<Product> products;
  private List<Advertisement> adv;
  private Designers designer;
  public String execute(){
	  System.out.println("showProduct**************"+data.appPath);
	  Arithmetic.getKey("webbei");
	  products = ps.getProducts();
	  adv = ps.getAllAdvertisements();
	  int size = products.size();
	  String real = ServletActionContext.getServletContext().getRealPath("/");
	  for(int i=0;i<size;i++){
		  String pUrl = products.get(i).getUrl();
		  String url = real+pUrl;
		  System.out.println("url:"+url);
		  products.get(i).setUrl(Arithmetic.getEncString(pUrl));
		  String newUrl="";
		try {
			newUrl = Thumbnail.saveImageAsJpg(url, 250);
			 products.get(i).setSmall(newUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
	  }
	  String username = (String) ActionContext.getContext().getSession().get("username");
	  designer = ps.getDesignerByUsername(username);
	  
	  System.out.println("designer:"+designer);
	  return "success";
  }
  
	public void setPs(ProductService ps) {
		this.ps = ps;
	}
	public List<Product> getProducts() {
		return products;
	}
	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public List<Advertisement> getAdv() {
		return adv;
	}

	public void setAdv(List<Advertisement> adv) {
		this.adv = adv;
	}

	public Designers getDesigner() {
		return designer;
	}

	public void setDesigner(Designers designer) {
		this.designer = designer;
	}

}
